package com.dopies.bsdmall.model;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;

import com.dopies.BeeFramework.model.BaseModel;
import com.dopies.BeeFramework.model.BeeCallback;
import com.dopies.bsdmall.protocol.SESSION;
import com.dopies.bsdmall.protocol.STATUS;
import com.external.androidquery.callback.AjaxStatus;

/**
 * 用户账户处理
 * 
 * @author wangwei
 * 
 */
public class AccountModel extends BaseModel {

	public String msg = "";

	public AccountModel(Context context) {
		super(context);
	}

	/**
	 * 用户积分兑现
	 */
	public void checkAccount(String score, String desc) {

		String url = ProtocolConst.ACCOUNT;

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {
			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				AccountModel.this.callback(url, jo, status);
				try {
					STATUS responseStatus = STATUS.fromJson(jo
							.optJSONObject("status"));
					if (responseStatus.succeed == 1) { // 兑换成功
						msg = jo.getString("data");

						// JSONArray dataJsonArray = jo.optJSONArray("data");
						// if (null != dataJsonArray && dataJsonArray.length() >
						// 0) {
						// JSONObject accountObjec = dataJsonArray
						// .getJSONObject(0);
						// msg = accountObjec.get("data").toString();
						// }
					}
					AccountModel.this.OnMessageResponse(url, jo, status);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		};

		SESSION session = SESSION.getInstance();

		JSONObject requestJsonObject = new JSONObject();

		Map<String, String> params = new HashMap<String, String>();
		try {
			requestJsonObject.put("session", session.toJson());
			requestJsonObject.put("type", "jifen");
			requestJsonObject.put("amount", score);
			requestJsonObject.put("desc", desc);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		params.put("json", requestJsonObject.toString());

		cb.url(url).type(JSONObject.class).params(params);
		ProgressDialog pd = new ProgressDialog(mContext);
		pd.setMessage("请稍后...");
		aq.progress(pd).ajax(cb);
	}
}
